import { request } from "./request"
import {BASE_URL, BASE_API, BEARER, HEADER_TOKEN} from "@/utils/constant.js";
import { global }  from "@/utils/index"


// 获取充值信息
export const getOrderInfo = () => {
	return request({
		url: 'v1/payOrders/getOrderInfo',
		method: 'POST',
	})
}

// 获取充值价格
interface payCombosProps{
	type?: string | number
	versionCode?: string | number
}
export const payCombos = (data?: payCombosProps) => {
	return request({
		url: 'v1/payCombos/list',
		method: 'GET',
		data
	})
}
// 支付提交订单
interface submitOrderParentProps{
		appType: 1 | 2
		channel?: string,
		orderSource: 'pc' | 'wap' | 'app'
		orderType: number
		payChannel: 1 | 2 | 3 //1微信2支付宝3苹果
		payComboId: string | number
		studentId: string | number
		tradeType: string
}
export const submitOrderParent = ({studentId, payComboId}) => {
	return request({
		url: 'v1/payOrders/submitOrderParent',
		method: 'POST',
		data: {
			"studentId":studentId,
			"orderType":"0",
			"orderSource":"app",
			"payComboId": payComboId,
			"payChannel":"1",
			"tradeType":"APP"
		}
	})
}



// 小程序支付
export const miniappPay =  (data) => {
	return request({
		url: 'v1/miniapp/pay',
		method: 'POST',
		data
	})
}


// 抖音发起支付订单
export const douyinPay =  async (opts) => {
	return uni.request({
		url:'https://testdouyin.sh985.com/stu-mob-mon-customer/api/v1/douyin/pay/pay',
		data: {
			...opts
		},
		method:"POST",
		header:{
			'content-type': 'application/json', // 默认值，可根据需要修改
			[HEADER_TOKEN]: 'Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxNzc3ODA3MzIzNSIsImF1dGhvcml0aWVzIjoiIiwiZXhwIjoxODE4MDU2NTg2fQ.wMeFcu9JCfk3Ecf9LrSTQVnbR5sYpQXls58u8rWPZCEB0JuIbmRr1vppJ-yrCvwLHi0HQEUUyRMkTRb-htN0HA'
		},
		success({data}) {
			console.log("支付成功", data)
			if(data.status !== "200"){
				uni.showToast({
					title:"请求支付失败",
					icon:"error"
				})
				return;
			}
			if(uni.getSystemInfoSync().uniPlatform === 'mp-toutiao'){
				tt.pay({
				  orderInfo: {
				    order_id: data.data.order_id,
				    order_token:data.data.order_token,
				  },
				  service: 5,
				  success(res) {
				    if (res.code == 0) {
				      // 支付成功处理逻辑，只有res.code=0时，才表示支付成功
				      // 但是最终状态要以商户后端结果为准
					  setTimeout(() => {
					  	uni.switchTab({
					  		url:"/pages/tabbar/mine/index"
					  	})
					  }, 1000)
				    }
				  },
				  fail(res) {
				    // 调起收银台失败处理逻辑
					uni.showToast({
						title:"支付失败",
						icon:"error"
					})
				  },
				});
			}
	
		},
		complete(res) {
			console.log("支付完成", res)
		},
		fail(res) {
			console.log("支付失败", res)
		}
	})
	
}
